import 'package:grocery_app_demo/api/entity/goods_list_res_entity.dart';
import 'package:grocery_app_demo/api/entity/login_res_entity.dart';
import 'package:grocery_app_demo/api/entity/shipper_list_res_entity.dart';
import 'package:grocery_app_demo/api/entity/supplier_list_by_type_res_entity.dart';
import 'package:grocery_app_demo/api/entity/wait_for_purchase_goods_entity.dart';
import 'package:grocery_app_demo/api/entity/warehouse_list_res_entity.dart';

class EntityFactory {
  static Map<String, dynamic Function(Map<String, dynamic> json)> funMap = {
    (LoginResEntity).toString(): LoginResEntity.fromJson,
    (SupplierListByTypeResEntity).toString():
        SupplierListByTypeResEntity.fromJson,
    (WarehouseListResEntity).toString(): WarehouseListResEntity.fromJson,
    (ShipperListResEntity).toString(): ShipperListResEntity.fromJson,
    (GoodsListResEntity).toString(): GoodsListResEntity.fromJson,
    (WaitForPurchaseGoodsEntity).toString():
        WaitForPurchaseGoodsEntity.fromJson,
  };

  static T generateOBJ<T>(Map<String, dynamic> json) {
    var fun = funMap[T.toString()];
    if (fun != null) {
      return Function.apply(fun, [json]) as T;
    } else {
      throw FormatException("未定义的类型： ${T.toString()}");
    }
  }
}
